{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 枚举类\n",
    "\n",
    "为枚举类型定义一个class类型，每个常量都是class的一个唯一实例."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jan => Month.Jan , 1\n",
      "Feb => Month.Feb , 2\n",
      "Mar => Month.Mar , 3\n",
      "Apr => Month.Apr , 4\n",
      "May => Month.May , 5\n",
      "Jun => Month.Jun , 6\n",
      "Jul => Month.Jul , 7\n",
      "Aug => Month.Aug , 8\n",
      "Sep => Month.Sep , 9\n",
      "Oct => Month.Oct , 10\n",
      "Nov => Month.Nov , 11\n",
      "Dec => Month.Dec , 12\n"
     ]
    }
   ],
   "source": [
    "from enum import Enum\n",
    "\n",
    "# 定义了一个Month类型的枚举类\n",
    "# 可以通过 Month.Jan引用一个常量\n",
    "# 常量默认从1开始计数\n",
    "Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))\n",
    "\n",
    "# 访问枚举类的成员\n",
    "for name, member in Month.__members__.items():\n",
    "    print(name, '=>', member, ',', member.value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要精确控制枚举值，可以从`enum`中派生处枚举类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Weekday.Mon:  Weekday.Mon\n",
      "Weekday['Tue']:  Weekday.Mon\n",
      "Weekday.Tue.value:  1\n",
      "Weekday(1):  Weekday.Mon\n",
      "Sun => Weekday.Sun\n",
      "Mon => Weekday.Mon\n",
      "Tue => Weekday.Tue\n",
      "Wed => Weekday.Wed\n",
      "Thu => Weekday.Thu\n",
      "Fri => Weekday.Fri\n",
      "Sat => Weekday.Sat\n"
     ]
    }
   ],
   "source": [
    "from enum import Enum, unique\n",
    "\n",
    "@unique # 帮我们检查有没有重复值\n",
    "class Weekday(Enum):\n",
    "    Sun = 0 # Sun的value被设定为0\n",
    "    Mon = 1\n",
    "    Tue = 2\n",
    "    Wed = 3\n",
    "    Thu = 4\n",
    "    Fri = 5\n",
    "    Sat = 6\n",
    "    \n",
    "# 访问这些枚举值\n",
    "print(\"Weekday.Mon: \", Weekday.Mon)\n",
    "print(\"Weekday['Tue']: \", Weekday['Mon'])\n",
    "print(\"Weekday.Tue.value: \", Weekday.Mon.value)\n",
    "print(\"Weekday(1): \", Weekday(1))\n",
    "\n",
    "for name, member in Weekday.__members__.items():\n",
    "    print(name, '=>', member)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
